﻿using Caliburn.Micro;
using IBE = Common.IllegalBehaviorException;
using LearnEnglishWords.Models;
using LearnEnglishWords.UI.ViewModels;
using LearnEnglishWords.UI.Views;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
using Accessors;

// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace LearnEnglishWords
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    public sealed partial class App
    {
        private WinRTContainer _container;

        public App()
        {
            InitializeComponent();
        }

        protected override async void Configure()
        {
            _container = new WinRTContainer();
            _container.RegisterWinRTServices();

            _container.PerRequest<StartViewModel>();

            var dbInitializer = new SQLite.EnWordsDbInitializer();
            await dbInitializer.CreateTables();

            _container.Instance<CircleAccessor>(new CircleAccessor(dbInitializer.CirclesRepo, dbInitializer.WordsRepo));
            _container.Singleton<CirclesModel>();

            
            _container.PerRequest<WordDemostrationViewModel>();
            _container.PerRequest<DashboardViewModel>();
            _container.PerRequest<CircleDetailsViewModel>();

            _container.PerRequest<EnglishCardViewModel>();
            _container.PerRequest<RussianCardViewModel>();
        }

        protected override object GetInstance(Type service, string key)
        {
            var instance = _container.GetInstance(service, key);
            if (instance != null)
                return instance;

            throw new Exception("Could not locate any instances.");
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);            
        }

        protected override void PrepareViewFirst(Frame rootFrame)
        {
            _container.RegisterNavigationService(rootFrame);
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            DisplayRootView<StartView>();
        }        
    }
}